package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 图片资源控制器
 */
@RestController
@RequestMapping("images")
public class ImagesController {
    /**
     * 图片上传接口
     * @param upload
     */
    @PostMapping
    public Map upload(MultipartFile upload){
        Map<String,Object> map = new HashMap();
        String url = null;
        //把图片传到指定的绝对路径
        try {
            url = UploadUtil.upload(upload, UploadUtil.PATH + "/upload");
            map.put("uploaded",1);
            map.put("url",url);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded",0);
            Map<String,String> temp = new HashMap();
            temp.put("message","上传失败!");
            map.put("error",temp);
        }
        return map;
    }
}
